<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="company">
              <a-input v-model="model.company" placeholder="请输入公司"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工厂" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="site">
              <a-input v-model="model.site" placeholder="请输入工厂"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="车间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workshop">
              <a-input v-model="model.workshop" placeholder="请输入车间"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产线" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <a-input v-model="model.line" placeholder="请输入产线"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产线代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入产线代码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产线描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入产线描述"></a-input>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-model-item label="产线所属群组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lineGroup">-->
          <!--              <a-input v-model="model.lineGroup" placeholder="请输入产线所属群组"  ></a-input>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="运行的工单数量" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="maxRunningPartno">
              <a-input-number v-model="model.maxRunningPartno" placeholder="请输入运行的工单数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="首检样品数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="boundarySample">
              <a-input-number v-model="model.boundarySample" placeholder="请输入首检样品数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="MesLineCfg_status"
                                 placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MesLineCfgForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      isEdit: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        line: [
          { required: true, message: '请输入产线!' }
        ],
        company: [
          { required: true, message: '请输入公司!' }
        ],
        site: [
          { required: true, message: '请输入工厂!' }
        ]
      },
      url: {
        add: '/basic/mesLineCfg/add',
        edit: '/basic/mesLineCfg/edit',
        queryById: '/basic/mesLineCfg/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.isEdit = false
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (JSON.stringify(record) !== '{}') {
        this.isEdit = true
      }
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.isEdit) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>